package com.example.demo.utils;

<<<<<<< HEAD
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import guru.nidi.graphviz.attribute.*;
import guru.nidi.graphviz.attribute.Color;
import guru.nidi.graphviz.attribute.Shape;
import guru.nidi.graphviz.engine.Format;
import guru.nidi.graphviz.engine.Graphviz;
import guru.nidi.graphviz.model.MutableGraph;
import guru.nidi.graphviz.model.MutableNode;
import guru.nidi.graphviz.model.Factory;
import guru.nidi.graphviz.model.Link;

import guru.nidi.graphviz.attribute.Style;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static guru.nidi.graphviz.model.Factory.mutGraph;
import static guru.nidi.graphviz.model.Factory.mutNode;

public class VisualizationUtils {
    /**
     * 使用graphviz生成依赖关系图，实现可视化生成
     *
     * @param dependencies 映射依赖关系，其中每个key是一个类/方法/Jar，value是依赖于该key的集合。
     * @param project 当前的项目对象，用于确定图片保存路径
     */
    public static void visualizeDependencies(Map<String, Set<String>> dependencies, Project project) {

        // 创建图对象壳子
        MutableGraph graph = mutGraph("dependencies").setDirected(true)
                //.graphAttrs().add(Rank.RankDir.LEFT_TO_RIGHT) // 节点从左到右排列
                .linkAttrs().add("splines", "ortho") // 使连接线更平滑
                .graphAttrs().add("ranksep", "1.0") // 设置节点之间的间距
                .nodeAttrs().add(Shape.RECTANGLE);

        // 防止重复节点
        Map<String, MutableNode> nodeMap = new HashMap<>();

        for (Map.Entry<String, Set<String>> entry : dependencies.entrySet()) {
            String source = entry.getKey();
            Set<String> targets = entry.getValue();

            // 获取源节点
            MutableNode sourceNode = nodeMap.computeIfAbsent(source, name -> mutNode(name).add(Shape.RECTANGLE));

            // 遍历目标节点
            for (String target : targets) {
                MutableNode targetNode = nodeMap.computeIfAbsent(target, name -> mutNode(name).add(Shape.RECTANGLE));
                // add edge
                graph.add(sourceNode.addLink(Link.to(targetNode).add(Style.BOLD)));
            }
        }

        // 获取项目根路径
        String projectPath = project.getBasePath();
        if (projectPath == null) {
            projectPath = ".";  // 如果无法获取项目路径，使用当前路径
        }
        String imagePath = projectPath + File.separator + "final_dependencies_result.png";
        File output = new File(imagePath);

        // 生成结果的图片文件
        try {
            Graphviz.fromGraph(graph)
                    .width(1200) // Increase width to improve clarity
                    .render(Format.PNG) // Change to SVG for higher clarity
                    .toFile(output);
            System.out.println("create successfully in :" + output.getAbsolutePath());
            EventQueue.invokeLater(() -> new ImageDisplayDialog(null, imagePath).setVisible(true));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
=======
import javax.swing.*;
import java.awt.*;
import java.util.Map;
import java.util.Set;

public class VisualizationUtils {
    public static void visualizeDependencies(Map<String, Set<String>> dependencies) {
        JFrame frame = new JFrame("Dependency Visualization");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(800, 600);

        JTextArea textArea = new JTextArea();
        dependencies.forEach((key, value) -> {
            textArea.append(key + " -> " + value + "\n");
        });

        JScrollPane scrollPane = new JScrollPane(textArea);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}
>>>>>>> ae04cf21c481035528eaf247571d600f597507d9
